package traveldream.ejb.EAOs;


import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.annotation.security.RolesAllowed;
import javax.ejb.EJBContext;


import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import travelDream.ejb.DTOs.UserDTO;
import travelDream.ejb.EAOs.DatiUtente;
import traveldream.ejb.entities.Group;
import traveldream.ejb.entities.User;

/**
 * Session Bean implementation class DatiUtenteEAO
 */
@Stateless
public class DatiUtenteEAO implements DatiUtente {

	@Resource			//riferimento al container
	private EJBContext context;
	
	@PersistenceContext
	EntityManager em;
	
    public DatiUtenteEAO() {
    }
    
    @Override
	@RolesAllowed({"CLIENTI"})
    public String getNomeCliente(){
    	return context.getCallerPrincipal().getName();	
    }

    @Override
	@RolesAllowed({"DIPENDENTI", "ADMIN"})
	public String getNomeDipendente(){
		return context.getCallerPrincipal().getName();
	}
    
    //trasforma in SHA256
    private String toSHA256(String chiaro){
    	String sha256password = new String();
    	try{
    		MessageDigest md;
			md = MessageDigest.getInstance("SHA-256");
			md.update(chiaro.getBytes(),0,chiaro.length());  //..
			sha256password = new BigInteger(1,md.digest()).toString(16);
    	} catch (NoSuchAlgorithmException e) {}
    	return sha256password;
    }
    
    //registra un nuovo dipendente
    @Override
    @RolesAllowed({"ADMIN"})
    public void creaDipendente (UserDTO user) {
    	user.setPassword(toSHA256(user.getPassword()));	//trasforma in SHA256
    	User nuovo = new User(user);
    	List<Group> gruppi = new ArrayList<Group>();
    	gruppi.add(new Group("DIPENDENTI"));
    	nuovo.setGroups(gruppi);
    	em.persist(nuovo);
    }
    
    //registra un nuovo cliente
    @Override
    public void creaCliente (UserDTO user) {
    	user.setPassword(toSHA256(user.getPassword()));	//trasforma in SHA256
    	User nuovo = new User(user);
    	List<Group> gruppi = new ArrayList<Group>();
    	gruppi.add(new Group("CLIENTI"));
    	nuovo.setGroups(gruppi);
    	em.persist(nuovo);
    }
    
    @Override
	@RolesAllowed({"CLIENTI"})
	public void updateCliente(UserDTO user) {
		em.merge(new User(user));
	}

    //true se l'utente esiste, false se non esiste
	@Override
	public boolean exists(String email) {
		Query query = em.createNamedQuery(User.FIND_BY_EMAIL, User.class);
		query.setParameter("email", email);
		try{
		query.getSingleResult();
		} catch(NoResultException e) {return false;}
		return true;
	}

	@Override
	@RolesAllowed({"CLIENTI"})
	public UserDTO findUser(String email) {
		Query query = em.createNamedQuery(User.FIND_BY_EMAIL, User.class);
		query.setParameter("email", email);
		User tmp = (User)query.getSingleResult();
		return tmp.entityToDTO();
	}

	
}
